package com.wl.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.wl.entity.Apply_01;
import com.wl.entity.Approve_02;
import com.wl.entity.Loginfo;
import com.wl.service.Apply_01Service;
import com.wl.service.Approve_02Service;
import com.wl.util.CommonMessage;
import com.wl.util.PageData;

@Controller
public class Apply01Controller {

	String filepath = "";

	// 注入01环节service
	@Autowired
	private Apply_01Service apply01s;

	// 注入02环节service
	@Autowired
	private Approve_02Service approve02s;

	/**
	 * 增加01环节数据 01环节提交给02环节增加数据
	 * 
	 * @param apply
	 * @param log
	 * @return
	 */
	@PostMapping(value = "addData01", produces = "application/json;charset=utf-8", consumes = "application/json")
	@ResponseBody
	public boolean addApply01Data(@RequestBody Apply_01 apply) {
		System.out.println(apply.getApplyattach());

		// 获取前台传过来的参数
		String flowid = apply.getFlowid();// 流程编号
		String dmno = apply.getNexthandler();// 02环节处理人
		String applyno = apply.getApplyno();// 申请人的工号

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String applydate = sdf.format(new Date());

		// 创建02环节对象
		Approve_02 approve02 = new Approve_02(1, flowid, dmno, null, null, null, 1, applyno, null);
		// 01环节提交时给02环节表里增加待审批的数据
		boolean f = approve02s.insert(approve02);
		System.out.println(f);

		// 附件路径
		apply.setApplyattach(filepath);
		apply.setApplydate(applydate);

		// 创建日志
		Loginfo log = new Loginfo();
		log.setPerson(applyno);
		log.setFlowid(flowid);
		log.setLogcontent("01物料申请环节提交");
		log.setLogdate(applydate);

		// 调用01环节service中的增加方法
		return apply01s.insert(apply, log);
	}

	/**
	 * 获取上传文件地址
	 * 
	 * @param file
	 * @return
	 */
	@PostMapping(value = "path")
	@ResponseBody
	public CommonMessage getFilePath(MultipartFile file) {

		// filepath = "E://" + file.getOriginalFilename();

		// 获取附件名
		filepath = file.getOriginalFilename();

		try {
			// 将附件存放在路径下
			file.transferTo(new File("E://" + file.getOriginalFilename()));
		} catch (IllegalStateException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 拼接文件存放路径
		filepath = "E://" + file.getOriginalFilename();
		CommonMessage cm = new CommonMessage();
		cm.setMsg(filepath);
		return cm;
	}

	/**
	 * 根据编号查询数据，用于其他环节审批时展示数据用
	 * 
	 * @param flowid
	 * @return
	 */
	@GetMapping(value = "selectbyflowid")
	@ResponseBody
	public Apply_01 selectByflowid(String flowid) {
		System.out.println(apply01s.selectByflowid(flowid));
		// 调用service中的查询方法
		return apply01s.selectByflowid(flowid);
	}

	/**
	 * 查询未处理流程并响应到页面 分页
	 * 
	 * @param page  当前页
	 * @param limit 每页记录数
	 * @param dmno  02环节处理人
	 * @return
	 */
	@GetMapping(value = "apply01nothand", produces = "application/json;charset=utf-8")
	@ResponseBody
	public PageData<Apply_01> getApply01Handler(@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "10") int limit,
			@RequestParam(value = "currenthandler", defaultValue = "") String currenthandler) {
		// 调用service中的显示未处理流程的方法
		PageData<Apply_01> pd = apply01s.selectNotHandByName(currenthandler, page, limit);
		System.out.println(pd);
		return pd;
	}

	/**
	 * 退回的01环节提交给02环节 修改状态
	 * 
	 * @param apply
	 * @param log
	 * @return
	 */
	@PostMapping(value = "updateData01", produces = "application/json;charset=utf-8", consumes = "application/json")
	@ResponseBody
	public boolean updateApply01Data(@RequestBody Apply_01 apply) {
		System.out.println(apply.getApplyattach());

		// 获取前台传过来的参数
		String flowid = apply.getFlowid();// 流程编号
		String dmno = apply.getNexthandler();// 02环节处理人
		String applyno = apply.getApplyno();// 申请人的工号
		int applystatus = apply.getApplystatus();// 获取流程状态

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String applydate = sdf.format(new Date());

		// 创建02环节对象
		Approve_02 approve02 = new Approve_02(1, flowid, dmno, null, null, null, 1, applyno, null);
		// 01环节提交时给02环节表里增加待审批的数据
		boolean f = approve02s.insert(approve02);
		System.out.println(f);

		// 附件路径
		apply.setApplyattach(filepath);
		apply.setApplydate(applydate);

		// 创建日志
		Loginfo log = new Loginfo();
		log.setPerson(applyno);
		log.setFlowid(flowid);
		log.setLogcontent("退回的01物料申请环节提交");
		log.setLogdate(applydate);

		// 调用01环节service中的修改方法
		return apply01s.updateStatusByFlowid(apply, log);
	}

	/**
	 * 01环节撤销
	 * 
	 * @param apply
	 * @param log
	 * @return
	 */
	@PostMapping(value = "cancelApply", produces = "application/json;charset=utf-8", consumes = "application/json")
	@ResponseBody
	public boolean cancelApply(@RequestBody Apply_01 apply) {
		System.out.println(apply.getApplyattach());

		// 获取前台传过来的参数
		String flowid = apply.getFlowid();// 流程编号
		String applyno = apply.getApplyno();// 申请人的工号
		int applystatus = apply.getApplystatus();// 获取流程状态

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String applydate = sdf.format(new Date());

		// 创建日志
		Loginfo log = new Loginfo();
		log.setPerson(applyno);
		log.setFlowid(flowid);
		log.setLogcontent("退回的01环节取消");
		log.setLogdate(applydate);

		// 调用01环节service中的修改方法
		return apply01s.updateStatusByFlowid(apply, log);
	}

	/**
	 * 查询我提交的流程并响应到页面 分页
	 * 
	 * @param page  当前页
	 * @param limit 每页记录数
	 * @param dmno  02环节处理人
	 * @return
	 */
	@GetMapping(value = "myapplyed", produces = "application/json;charset=utf-8")
	@ResponseBody
	public PageData<Apply_01> getMyApply01(@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "10") int limit,
			@RequestParam(value = "applyno", defaultValue = "") String applyno) {
		// 调用service中的显示未处理流程的方法
		PageData<Apply_01> pd = apply01s.selectMyApplyByName(applyno, page, limit);
		System.out.println(pd);
		return pd;
	}

	/**
	 * 所有申请  多条件搜索  分页
	 * 
	 * @param page
	 * @param limit
	 * @return
	 */
	@GetMapping(value = "allapply", produces = "application/json;charset=utf-8")
	@ResponseBody
	public PageData<Apply_01> getAllApply01(@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "10") int limit,
			@RequestParam(value = "searchflowid", defaultValue = "") String searchflowid,
			@RequestParam(value = "searchmaterial", defaultValue = "") String searchmaterial,
			@RequestParam(value = "searchapply", defaultValue = "") String searchapply,
			@RequestParam(value = "searchdept", defaultValue = "") String searchdept,
			@RequestParam(value = "searchdatebegin", defaultValue = "") String searchdatebegin,
			@RequestParam(value = "searchdateend", defaultValue = "") String searchdateend) {
		// 调用service中的显示未处理流程的方法
		PageData<Apply_01> pd = apply01s.selectBySearch(searchflowid, page, limit, searchmaterial, searchapply, searchdept, searchdatebegin, searchdateend);
		System.out.println(pd);
		return pd;
	}
	
	/**
	 * 查询用料数量，用于页面展示数据
	 * @param flowid
	 * @return
	 */
	@GetMapping(value = "getCountByApply")
	@ResponseBody
	public int getCountByApply() {	
		return apply01s.getCountByApplyOne();
	}
	
	
	/**
	 * 按照部门查找申请单数  欢迎页面柱状图
	 * @return
	 */
	@GetMapping(value = "countbydept")
	@ResponseBody
	public List<Integer> getCountByDept() {	
		int n1 =  apply01s.getCountByDept("工程部");
		int n2 =  apply01s.getCountByDept("技术部");
		int n3 =  apply01s.getCountByDept("采购部");
		int n4 =  apply01s.getCountByDept("质检部");
		int n5 =  apply01s.getCountByDept("生管部");
		int n6 =  apply01s.getCountByDept("财务部");
		int n7 =  apply01s.getCountByDept("销售部");
		
		List<Integer> list = new ArrayList<Integer>();
		
		list.add(n1);
		list.add(n2);
		list.add(n3);
		list.add(n4);
		list.add(n5);
		list.add(n6);
		list.add(n7);
		
		return list;
	}
	
	
	/**
	 * 按照部门查找用料总数  欢迎页面环形图
	 * @return
	 */
	@GetMapping(value = "chartbyapprove")
	@ResponseBody
	public List<Integer> getmetrialCountByDept() {	
		int n1 =  apply01s.getmetrialCountByDept("工程部");
		int n2 =  apply01s.getmetrialCountByDept("技术部");
		int n3 =  apply01s.getmetrialCountByDept("采购部");
		int n4 =  apply01s.getmetrialCountByDept("质检部");
		int n5 =  apply01s.getmetrialCountByDept("生管部");
		int n6 =  apply01s.getmetrialCountByDept("财务部");
		int n7 =  apply01s.getmetrialCountByDept("销售部");
		
		List<Integer> list = new ArrayList<Integer>();
		
		list.add(n1);
		list.add(n2);
		list.add(n3);
		list.add(n4);
		list.add(n5);
		list.add(n6);
		list.add(n7);
		
		return list;
	}

}
